package com.uchengwang.ui.cm_ui.me.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.cycle.bean.RyGroupBean;
import com.uchengwang.ui.cm_ui.rychat.manager.RyGroupManager;
import com.uchengwang.ui.cm_ui.umsg.adapter.GroupAdapter;
import com.uchengwang.ui.cm_ui.uu.ui.GroupNotifyActivity;

import java.util.List;

/**
 * Created by U城网 on 2017/6/27.
 * 我的群组界面
 */

public class MyGroupsActivity extends BaseWithTitleActivity{

    private RecyclerView group_rv;
    private GroupAdapter adapter;
    private TextView fm_group_noNet;
    private PtrClassicFrameLayout mSwipeRefreshLayout;
    private RecyclerAdapterWithHF mAdapter;
    private int page =1, totlaPagers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_group_layou);
        setTitle("我的群组");

        setRightImage(80, R.drawable.speek_image_notice, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MyGroupsActivity.this, GroupNotifyActivity.class));
            }
        });


        initView();
    }

    private void initView() {
        group_rv = (RecyclerView) findViewById(R.id.fm_group_rv);
        fm_group_noNet = (TextView) findViewById(R.id.fm_group_noNet);
        mSwipeRefreshLayout = (PtrClassicFrameLayout) findViewById(R.id.fm_group_layout);
        initData();
    }

    private void initData() {
        adapter = new GroupAdapter(this);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        group_rv.setLayoutManager(new LinearLayoutManager(this));
        group_rv.setAdapter(mAdapter);

        mSwipeRefreshLayout.setAutoLoadMoreEnable(true);
        mSwipeRefreshLayout.setLoadMoreEnable(true);

        mSwipeRefreshLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                mSwipeRefreshLayout.autoRefresh(true);
            }
        }, 150);

        mSwipeRefreshLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page =1;
                getMyGroup(true);
            }
        });

        mSwipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totlaPagers){
                    //当前请求页大于总页数，不再请求
                    getMyGroup(false);
                } else {
                    mSwipeRefreshLayout.loadMoreComplete(false);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (BaseConfig.isRelease){
            mSwipeRefreshLayout.postDelayed(new Runnable() {

                @Override
                public void run() {
                    mSwipeRefreshLayout.autoRefresh(true);
                }
            }, 150);
        }
    }

    /**
     * 获取我的群组信息
     * */
    private void getMyGroup(final boolean isRefresh){
        RyGroupManager.getManager().getMyGroups(this, page, 10, 0, new OnHttpResultListener<RyGroupBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<RyGroupBean> list) {
                initGroupData(isRefresh, status, total, list);
            }
        });
    }

    private void initGroupData(boolean isRefresh, int stutas, int total, List<RyGroupBean> list){
        totlaPagers =total;
        if (isRefresh){
            mSwipeRefreshLayout.refreshComplete();
        }else {
            mSwipeRefreshLayout.loadMoreComplete(true);
        }
        if (stutas ==1){
            adapter.getData().clear();
            adapter.notifyDataSetChanged();
        }
        if (list != null && list.size() >0){
            if (isRefresh){
                adapter.setData(list);
            }else {
                adapter.getData().addAll(list);
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        BaseConfig.isRelease = false;
    }
}
